const http = require('http')
const querustring = require('querystring')
const url = require('url')
global.l = console.log
const server = http.createServer((request, response) => {
  request.setEncoding('utf8')
  request.on('data', (data) => {
    console.log('服务器接收到数据:', data)
    response.write("确认数据,你好");

    // console.log(querustring.parse(data));
  })
  request.on('end', () => {
    response.end()
  })
  if (request.url !== '/favicon.ico') {
    // response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
    // response.writeHead(200, {
    //   'Content-Type': 'text/plain;charset=utf-8',
    //   'Access-Control-Allow-Origin': '*'
    // });
    // if (response.headersSent) { console.log('响应头已发送'); }
    // response.write("你好");
    // let url_part = url.parse(request.url)
    // console.log(url_part);
    // switch (url_part.pathname) {
    //   case '/':
    //   case '/index.html':
    //     response.write('<html><head><meta charset="utf-8"></meta></head></html>')
    //     response.write(`<body><div>当前页面为:首页</div>`)
    //     response.write(`<input type="button" value="获取数据" onclick="getData">
    //     <div id="getData"></div>`)
    //     response.write(`</body>`)
    //     break;
    //   case '/main.html':
    //     response.write('<html><head><meta charset="utf-8"></meta></head></html>')
    //     response.write(`<body><div>当前页面为:${url_part.pathname}</div></body>`)
    //     break;
    // }

  }
}).listen(3000, 'localhost', () => {
  l('服务启动成功', server.address());
})
// server.on('connection', (socket) => {
//   l('客户端建立连接')
// })
//服务定时器
// server.setTimeout(5 * 60 * 1000, function (socket) {
//   l('服务器超时')
// })
server.on('error', (err) => {
  l('服务出现异常', err);
})
server.on('close', (err) => {
  l('服务关闭');
  l(err)
})